package concurrent;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 阻塞队列
 * 遵循FIFO原则
 *
 *
 * @author fengpeng
 * @date 2019-07-29  14:08
 */
@Slf4j
public class MyBlockingQueue {

    /**
     * 使用LinkedList作为阻塞队列的存储容器
     */
    private final List<Object> list = Lists.newLinkedList();

    /**
     * 阻塞队列的长度
     */
    private volatile int size = 0;

    /**
     * 最大容量
     */
    private final int maxSize;

    /**
     * 最小值
     */
    private static final int minSize = 0;

    /**
     * 最大等待时间（单位：ms）
     * 默认为：1 秒
     */
    private volatile  long maxWaiteTime = 1000L;

    public MyBlockingQueue(int maxSize) {
        this.maxSize = maxSize;
    }

    public MyBlockingQueue(int maxSize,long maxWaiteTime){
        this.maxSize = maxSize;
        this.maxWaiteTime = maxWaiteTime;
    }

    public int size(){
        return size;
    }

    /**
     * 同步锁
     */
    Object lock = new Object();

    /**
     * 写队列
     *
     * @param o 插入参数
     */
    public void put(Object o){
        synchronized (lock){
            long startTime = System.currentTimeMillis();
            while(size == maxSize){
                if (System.currentTimeMillis() - startTime <= maxWaiteTime){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else{
                    throw new RuntimeException("put waite time out...!");
                }
            }
            size++;
            list.add(o);
            log.info("插入参数:{}",o);
            log.info("当前队列大小为:{}",size);
            lock.notify();
        }
    }

    /**
     * 读队列
     */
    public Object take(){
        Object o;
        synchronized (lock){
            long startTime = System.currentTimeMillis();
            while(size == minSize){
                if (System.currentTimeMillis() - startTime <= maxWaiteTime){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else{
                    throw new RuntimeException("take waite time out...!");
                }
            }
            size--;
            o = list.remove(0);
            log.info("取出参数:{}",o);
            log.info("当前队列大小为:{}",size);
            lock.notify();
        }
        return o;
    }

    public static void main(String[] args){
        final MyBlockingQueue m = new MyBlockingQueue(5);
        m.put("a");
        m.put("b");
        m.put("c");
        m.put("d");
        m.put("e");
       log.info("当前元素个数：{}" ,m.size());


        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                m.put("h");
                m.put("i");
            }
        }, "t1");

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                     m.take();
                    //System.out.println("被取走的元素为：" + t1);
                    //log.info("被取走的元素为：()" + t1)
                    Thread.sleep(10000);
                     m.take();
                    //System.out.println("被取走的元素为：" + t2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "t2");

        t1.start();


        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        t2.start();
    }
}
